//decl: v4l2_buffer, v4l2_capability, v4l2_input, V4L2_BUF_TYPE_XXX, 
#include <linux/videodev2.h>
//decl: open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h> //decl: fprintf, stderr, printf
#include <errno.h> //decl: erno
#include <sys/ioctl.h> //ioctl


int main(int argc, char *argv[]) {
    int cam_fd;
    const char *cam_path = "/dev/video0";
    struct v4l2_buffer cam_buf;
    struct v4l2_capability cam_cap;
    struct v4l2_input cam_inp;    

    //打开摄像头
    cam_fd = open(cam_path, "O_RDWR");
    if(m_fd == -1) {
        int erno = errno;
        perrno("perror");
        fprintf(stderr, "Open camera %s failed!\n", /dev/video0);
        return erno;
    }
    //查询摄像头能力，主要是用来判定是否支持抓帧和获取方式
    memset(&cam_cap, 0, sizeof(cam_cap));
    memset(&cam_buf, 0, sizeof(cam_buf));
    ioctl(cam_fd, VIDIOC_QUERYCAP, &cam_cap);
    printf("Camera:%s, path:%s, driver:%s, bus:%s, version:%#010X, caps:%#010X",
           cam_cap.card, cam_path, cam_cap.driver, cam_cap.bus, cam_cap.version,
           cam_cap.capabilities);
    //根据capture类型设置buf类型
    //所有cap都只占1bit, 因此可直接求与判定
    if(cam_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
        cam_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    }else if(cam_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
        cam_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
    }

    //选择输入源(可多个源，一般只有一个可跳过)
    cam_inp.index = 0;
    cap_inp.type = V4L2_INPUT_TYPE_CAMERA;
    ioctl(cam_fd, VIDIOC_S_INPUT, &cam_inp);
    
    
}
